﻿Public Class Agendamento
    Public loctipo As String
    Public parametro As String
    Dim objBBL As New BLLAgendamento
    Dim tabItens As New DataTable
    Dim operacao As String
    Dim codsala As Integer

#Region "Atualizações"
    Sub limpa()
        txtcodigo.Text = ""
        txtcodpessoa.Text = ""
        txtnome.Text = ""
        txtsala.Text = ""
        txtcodsala.Text = ""
        dgcadastro.Columns.Clear()
        txtrecurso.Text = ""
        txtcodrec.Text = ""
        txtpatrimonio.Text = ""
        cbcurso.SelectedIndex = -1
    End Sub
    Function validaItem() As Boolean
        Dim ret As Boolean
        ret = False
        For Each linha As DataRow In tabItens.Rows
            If (linha("Codigo") = txtcodrec.Text) Then
                ret = True
            End If
        Next
        Return ret
    End Function
    Sub carrega_gridcadastro()
        tabItens = New DataTable
        'adicionei colunas
        tabItens.Columns.Add("Codigo", GetType(String))
        tabItens.Columns.Add("Nome", GetType(String))
        tabItens.Columns.Add("Patrimonio", GetType(String))
        ' tabItens.Columns.Add("pec_dtadesao", GetType(Date))
        'vinculei datagridview
        dgcadastro.DataSource = tabItens
        dgcadastro.Columns("Codigo").HeaderText = "Codigo"
        dgcadastro.Columns("Codigo").Width = 50
        dgcadastro.Columns("nome").HeaderText = "Descrição"
        dgcadastro.Columns("nome").Width = 145
        dgcadastro.Columns("Patrimonio").HeaderText = "Patrimônio"
        dgcadastro.Columns("Patrimonio").Width = 100
    End Sub
    Sub Carrega_Selecao()
        Dim linha As DataRow
        linha = tabItens.NewRow
        linha("Codigo") = txtcodrec.Text
        linha("nome") = txtrecurso.Text
        linha("Patrimonio") = txtpatrimonio.Text
        tabItens.Rows.Add(linha)
        tabItens.AcceptChanges()
    End Sub
    Sub carregapropriedades()
        Try
            objBBL.limpa()
            If (txtcodigo.Text = "") Then
                objBBL.pagecodigo = 0
            Else
                objBBL.pagecodigo = Val(txtcodigo.Text)
            End If
            objBBL.pobjCliente = New BLLSolicitante(txtcodpessoa.Text, cbpessoa.SelectedItem)
            objBBL.pdata = dtpdata.SelectionRange.Start
            objBBL.pcodsala = txtcodsala.Text
            objBBL.pusuario = fMenu.codigo
            objBBL.pcurso = cbcurso.SelectedItem
            objBBL.phfinal = dtpfim.Text
            objBBL.phinicial = dtpinicio.Text
            objBBL.pcurso = cbcurso.SelectedItem
            Dim obj As BLLItensRecurso
            For Each linha As DataRow In tabItens.Rows
                obj = New BLLItensRecurso
                obj.pitecodigo = objBBL.pagecodigo
                obj.pobjRecurso = New BLLRecurso(linha("Codigo").ToString)
                objBBL.plRecurso.Add(obj)
            Next
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
#End Region



    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        fMenu.Show()
        Me.Close()
    End Sub

    Private Sub Agendamento_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        limpa()
        cbpessoa.SelectedIndex = 1
        dtpdata.TodayDate = Date.Now
        dtpdata.ShowTodayCircle = True
        carrega_gridcadastro()
        balterar.Enabled = False
        bsalvar.Enabled = True
        txtcodigo.Text = MenuAgendamento.codigo
    End Sub

    Private Sub bLocPessoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bLocPessoa.Click
        LocalizaPessoa.ShowDialog()
        txtnome.Text = LocalizaPessoa.nome
        txtcodpessoa.Text = LocalizaPessoa.codigo
    End Sub

    Private Sub bLocSala_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bLocSala.Click
        LocalizaSala.ShowDialog()
        txtsala.Text = LocalizaSala.descricao
        txtcodsala.Text = LocalizaSala.codigo
        Dim ret As Boolean
        If txtsala.Text <> "" Then
            ret = objBBL.ValidaSala(WeekdayName(Weekday(dtpdata.SelectionRange.Start)), dtpinicio.Text, dtpfim.Text, txtcodsala.Text)
            If ret = True Then
                carrega_gridcadastro()
            Else
                MsgBox("Sala não pode ser inserido nesse horário/dia da semana")
                txtsala.Text = ""
                txtcodsala.Text = ""
                dtpinicio.Select()
            End If

        End If

        'txtcodsala.Text = LocalizaSala.codigo
    End Sub




    Private Sub txtnome_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtnome.KeyPress
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            e.Handled = True
            Try

                LocalizaPessoa.loctipo = cbpessoa.SelectedItem
                LocalizaPessoa.parametro = txtnome.Text
                LocalizaPessoa.ShowDialog()
                txtnome.Text = LocalizaPessoa.nome
                txtcodpessoa.Text = LocalizaPessoa.codigo
                parametro = ""
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub txtsala_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtsala.KeyPress
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            e.Handled = True
            Try
                LocalizaSala.sala = txtsala.Text
                LocalizaSala.ShowDialog()
                txtsala.Text = LocalizaSala.descricao
                txtcodsala.Text = LocalizaSala.codigo
                Dim ret As Boolean
                If txtsala.Text <> "" Then
                    ret = objBBL.ValidaSala(WeekdayName(Weekday(dtpdata.SelectionRange.Start)), dtpinicio.Text, dtpfim.Text, txtcodsala.Text)
                    If ret = True Then
                        carrega_gridcadastro()
                    Else
                        MsgBox("Sala não pode ser inserido nesse horário/dia da semana")
                        txtsala.Text = ""
                        txtcodsala.Text = ""
                        dtpinicio.Select()
                    End If

                End If

            Catch ex As Exception

            End Try
        End If
        parametro = ""
    End Sub


    Private Sub badicionar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles badicionar.Click

        If txtpatrimonio.Text <> "" Then
            If (Not validaItem()) Then
                Carrega_Selecao()
            Else
                MessageBox.Show("Recurso já inserido.")
            End If
            txtrecurso.Text = ""
            txtpatrimonio.Text = ""
            txtcodrec.Text = ""
        Else
            MsgBox("Insira um Recurso Válido", MsgBoxStyle.Critical)

        End If
    End Sub

    Private Sub bremover_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bremover.Click
        Dim indice As Integer
        indice = -1
        'percorrer todas as linhas selecionadas (1)
        'para buscar o indice da linha selecionada
        For Each linha As DataGridViewRow In dgcadastro.SelectedRows
            If (Not linha.Cells("Codigo") Is Nothing) Then
                indice = linha.Index
            End If
        Next
        If (indice <> -1) Then
            'remover          
            tabItens.Rows.RemoveAt(indice)
            tabItens.AcceptChanges()
        Else
            MessageBox.Show("Selecione uma linha a ser removida")
        End If
    End Sub

    Private Sub bsalvar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bsalvar.Click
        Dim ret As Boolean
        Try
            If FormatDateTime(dtpdata.SelectionRange.Start, vbShortDate) >= FormatDateTime(Date.Now, vbShortDate) Then '
                If FormatDateTime(dtpinicio.Text, vbShortTime) < FormatDateTime(dtpfim.Text, vbShortTime) Then '
                    If cbcurso.SelectedIndex <> -1 Then '
                        If txtnome.Text <> "" And txtsala.Text <> "" Then '
                            If dgcadastro.RowCount > 0 Then '
                                ret = objBBL.ValidaInsere(dtpdata.SelectionRange.Start, txtcodsala.Text, dtpinicio.Text, dtpfim.Text)
                                If ret = True Then
                                    carregapropriedades()
                                    ret = objBBL.inserir()
                                    MenuAgendamento.atualizaagendamento()
                                    MenuAgendamento.filtroagendamento()
                                    If ret = True Then
                                        MsgBox("Agendamento realizado com sucesso", MsgBoxStyle.Information)
                                        limpa()
                                    End If
                                Else
                                    MsgBox("Agendamento não permitido", MsgBoxStyle.Critical)
                                End If
                            Else
                                MsgBox("Favor inserir o recurso a ser agendado", MsgBoxStyle.Critical)
                            End If
                        Else
                            MsgBox("Favor preencher todos os campos", MsgBoxStyle.Information)
                        End If
                    Else
                        MsgBox("Selecionar Curso", MsgBoxStyle.Information)
                    End If
                Else
                    MsgBox("Horário de início menor que horário final", MsgBoxStyle.Critical)
                End If
            Else
                MsgBox("Data do Agendamento errado", MsgBoxStyle.Critical)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    
    End Sub


    Private Sub dtpdata_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles dtpdata.DateChanged
        dtpdata.TodayDate = dtpdata.SelectionRange.Start
        dtpdata.ShowTodayCircle = True

    End Sub


    Private Sub cbpessoa_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbpessoa.SelectedIndexChanged
        cbpessoa.Refresh()
        txtcodpessoa.Text = ""
        txtnome.Text = ""
    End Sub

    Private Sub cbpessoa_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cbpessoa.KeyPress

        e.Handled = True
    End Sub

    Private Sub cbcurso_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cbcurso.KeyPress
        e.Handled = True
    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox(dtpdata.SelectionRange.Start)
    End Sub

    Private Sub txtrecurso_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtrecurso.KeyPress
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            e.Handled = True
            Try
                LocalizaRecurso.recurso = txtrecurso.Text
                LocalizaRecurso.ShowDialog()
                txtcodrec.Text = LocalizaRecurso.codigo
                txtrecurso.Text = LocalizaRecurso.nome
                txtpatrimonio.Text = LocalizaRecurso.patrimonio
                Dim ret As Boolean
                If txtrecurso.Text <> "" Then
                    ret = objBBL.ValidaSala(WeekdayName(Weekday(dtpdata.SelectionRange.Start)), dtpinicio.Text, dtpfim.Text, txtcodsala.Text)
                    If ret = True Then
                        ' carrega_gridcadastro()
                    Else
                        MsgBox("Sala não pode ser inserido nesse horário/dia da semana")
                        txtsala.Text = ""
                        txtcodsala.Text = ""

                    End If

                End If

            Catch ex As Exception

            End Try
        End If
        parametro = ""
    End Sub

    Private Sub txtcodigo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtcodigo.TextChanged
        Dim tab As DataTable
        Dim data As DateTime
        Try
            If txtcodigo.Text <> "" Then
                bsalvar.Enabled = False
                balterar.Enabled = True
                cbpessoa.SelectedIndex = -1
                tab = objBBL.LocalizaAgendamento(txtcodigo.Text)
                dtcadastro.Text = tab.Rows(0)("age_datacad").ToString()
                cbpessoa.SelectedItem = tab.Rows(0)("agd_desc").ToString()
                txtcodpessoa.Text = tab.Rows(0)("agd_codigo").ToString()
                txtnome.Text = tab.Rows(0)("agd_nome").ToString()
                dtpinicio.Text = tab.Rows(0)("age_horainicial").ToString()
                dtpfim.Text = tab.Rows(0)("age_horafinal").ToString()
                txtcodsala.Text = tab.Rows(0)("sal_codigo").ToString()
                txtsala.Text = tab.Rows(0)("sal_descricao").ToString()
                cbcurso.SelectedItem = tab.Rows(0)("age_curso").ToString()
                data = tab.Rows(0)("age_data").ToString()
                dtpdata.TodayDate = data.ToShortDateString
                dtpdata.ShowTodayCircle = True
                data = DateTime.Parse(data)
                tab = Nothing
                tab = objBBL.LocalizaRecurso(txtcodigo.Text, data)
                For Each linha As DataRow In tab.Rows
                    txtcodrec.Text = linha("rec_codigo").ToString()
                    txtrecurso.Text = linha("rec_descricao").ToString()
                    txtpatrimonio.Text = linha("rec_patrimonio").ToString()
                    badicionar_Click(sender, e)
                Next
            Else
                balterar.Enabled = False
                bsalvar.Enabled = True
            End If
        Catch ex As Exception
            tab = Nothing
        End Try


    End Sub

    Private Sub bLocRecurso_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bLocRecurso.Click
        LocalizaRecurso.ShowDialog()
        txtcodrec.Text = LocalizaRecurso.codigo
        txtrecurso.Text = LocalizaRecurso.nome
        txtpatrimonio.Text = LocalizaRecurso.patrimonio
        Dim ret As Boolean
        If txtrecurso.Text <> "" Then
            ret = objBBL.ValidaRecurso(dtpdata.SelectionRange.Start, txtcodrec.Text, dtpinicio.Text, dtpfim.Text)
            If ret = False Then
                MsgBox("Recurso não pode ser inserido nesse Horário/Data", MsgBoxStyle.Critical)
                txtpatrimonio.Text = ""
                txtrecurso.Text = ""
                txtcodrec.Text = ""
            End If
        End If
    End Sub

    Private Sub balterar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles balterar.Click
        Dim ret As Boolean
        If MessageBox.Show("Confirma Alteração ?", "", MessageBoxButtons.YesNo) = vbYes Then
            If dgcadastro.RowCount > 0 Then
                ret = objBBL.ValidaAlterar(dtpdata.SelectionRange.Start, txtcodsala.Text, dtpinicio.Text, dtpfim.Text, txtcodigo.Text)
                If ret = True Then
                    carregapropriedades()
                    ret = objBBL.alterar()
                    MenuAgendamento.atualizaagendamento()
                    MenuAgendamento.filtroagendamento()
                    If ret = True Then
                        MsgBox("Agendamento Alterado com sucesso", MsgBoxStyle.Information)
                        limpa()
                    End If
                Else
                    MsgBox("Alteração não permitido", MsgBoxStyle.Critical)
                End If
            End If
        End If

    End Sub
End Class